package com.net.framework.help.utils;

import android.annotation.SuppressLint;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 实体工具类
 * 
 * @author sai.you
 * 
 */
@SuppressLint({ "SimpleDateFormat", "UseValueOf" })
public final class EntityUtil {
	public final static Long getLongValue(Object object) {
		if (object == null) {
			return null;
		}
		if (object instanceof Long) {
			return (Long) object;
		} else if (object instanceof BigDecimal) { // 濡傛灉鏄疊igDecimal
			BigDecimal bd = (BigDecimal) object;
			return new Long(bd.longValue());
		} else {
			String str_value = object.toString(); //
			return new Long(StringUtil.isBlank(str_value) ? "0" : str_value); //
		}
	}

	public final static Double getDoubleValue(Object object) {
		if (object == null) {
			return null;
		} else if (object instanceof Double) {
			return (Double) object;
		} else if (object instanceof BigDecimal) { // 濡傛灉鏄疊igDecimal
			BigDecimal bd = (BigDecimal) object;
			return new Double(bd.doubleValue());
		} else {
			return new Double(StringUtil.isBlank(object.toString()) ? "0" : object.toString());
		}
	}

	public final static String getStringValue(Object o) {
		if (o == null) {
			return null;
		}

		if (o instanceof String) {
			return o.toString();
		} else if (o instanceof BigDecimal) {
			return o.toString();
		} else if (o instanceof java.sql.Timestamp) {
			java.sql.Timestamp ts = (java.sql.Timestamp) o;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String str_date = sdf.format(new Date(ts.getTime())); //
			return str_date;
		} else if (o instanceof Integer) {
			return o.toString();
		} else if (o instanceof Long) {
			return o.toString();
		} else if (o instanceof Double) {
			return o.toString();
		} else if (o instanceof Date) {
			Date date = (Date) o;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String str_date = sdf.format(date); //
			return str_date;
		} else {
			return o.toString();
		}
	}

	public final static Integer getIntegerValue(Object o) {
		if (o == null) {
			return -999;
		}

		if (o instanceof Integer) {
			return (Integer) o;
		} else if (o instanceof BigDecimal) { // 濡傛灉鏄疊igDecimal
			BigDecimal bd = (BigDecimal) o;
			return new Integer(bd.intValue());
		} else {
			String str_value = o.toString();
			int li_pos = str_value.indexOf(".");
			if (li_pos >= 0) {
				str_value = str_value.substring(0, li_pos);
			}
			return new Integer(str_value);
		}
	}

	public final static Float getFloatValue(Object o) {
		if (o == null) {
			return 0f;
		}

		if (o instanceof Float) {
			return (Float) o;
		} else if (o instanceof BigDecimal) { // 濡傛灉鏄疊igDecimal
			BigDecimal bd = (BigDecimal) o;
			return new Float(bd.intValue());
		} else {
			String str_value = o.toString();

			return new Float(StringUtil.isBlank(str_value) ? "0" : str_value);
		}
	}

	public final static Date getDateValue(Object o) {

		if (o == null) {
			return null;
		}

		if (o instanceof java.sql.Timestamp) {
			java.sql.Timestamp ts = (java.sql.Timestamp) o;
			return new Date(ts.getTime());
		} else if (o instanceof Date) {
			return (Date) o;
		} else if (o instanceof Long) {
			return new Date((Long) o);
		} else {
			String str_value = o.toString().trim();
			try {
				if (str_value.length() == 10) { // 濡傛灉鏄�0浣�
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
					Date date = sdf.parse(str_value);
					return new Date(date.getTime());
				} else if (str_value.length() == 19) { // 濡傛灉鏄�9浣�
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
					Date date = sdf.parse(str_value);
					return new Date(date.getTime());
				} else {
					return null;
				}
			} catch (Exception ex) {
				ex.printStackTrace();
				return null;
			}
		}
	}
}
